package com.example.dao.daoImple;

import com.example.dao.FocusDao;
import com.example.entity.Focus;
import com.example.util.JDBCUtil;

import java.util.List;

public class FocusDaolmpl implements FocusDao {
    @Override
    public Focus isFocus(Long userId, Long authordId) {
        String sql = "SELECT user_id,author_id FROM tb_focus WHERE user_id = ? AND author_id = ?";
        List<Focus> forList = JDBCUtil.getForList(Focus.class,sql,userId, authordId);
        return forList==null || forList.isEmpty()? null:forList.get(0);
    }

    @Override
    public int focus(Focus focus) {
        String sql = "INSERT INTO tb_focus (user_id,author_id) VALUES (?,?)";
        return JDBCUtil.ALLCUD(sql,focus.getUserId(),focus.getAuthorId());
    }

    @Override
    public int cancelFocus(Long userId, Long authorId) {
        String sql = "DELETE FROM tb_focus WHERE user_id = ? AND author_id = ?";
        return JDBCUtil.ALLCUD(sql,userId,authorId);
    }

    @Override
    public int updateFocus(Long userId){
        String sql = "UPDATE tb_user u \n" +
                "SET u.focus = (SELECT COUNT(1) FROM tb_focus f WHERE f.user_id = ?)\n" +
                "WHERE u.id = ?";
        return JDBCUtil.ALLCUD(sql,userId,userId);
    }

    @Override
    public int addFan(Long userId) {
        String sql = "\n" +
                "UPDATE tb_user \n" +
                "SET fans = fans + 1\n" +
                "WHERE id = ?";
        return JDBCUtil.ALLCUD(sql,userId);
    }

    @Override
    public int reduceFan(Long userId) {
        String sql = "\n" +
                "UPDATE tb_user \n" +
                "SET fans = fans - 1\n" +
                "WHERE id = ?";
        return JDBCUtil.ALLCUD(sql,userId);
    }
}
